package com.yanfan.building.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.ibms.common.constant.IbmsConstant;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanfan.building.mapper.AreaBuildingFloorMapper;
import com.yanfan.building.domain.AreaBuildingFloor;
import com.yanfan.building.service.IAreaBuildingFloorService;

/**
 * 区域楼栋楼层Service业务层处理
 *
 * @author genluo
 * @date 2024-10-09
 */
@Service
public class AreaBuildingFloorServiceImpl extends ServiceImpl<AreaBuildingFloorMapper, AreaBuildingFloor> implements IAreaBuildingFloorService {
    @Autowired
    private AreaBuildingFloorMapper areaBuildingFloorMapper;


    /**
     * 查询区域楼栋楼层列表
     *
     * @param areaBuildingFloor 区域楼栋楼层
     * @return 区域楼栋楼层
     */
    @Override
    public List<AreaBuildingFloor> queryPageList(AreaBuildingFloor areaBuildingFloor) {
        LambdaQueryWrapper<AreaBuildingFloor> lqw = buildQueryWrapper(areaBuildingFloor);
        return areaBuildingFloorMapper.selectList(lqw);
        //连表查询可用该方法，在此方法SQL语句上进行扩展
        //return areaBuildingFloorMapper.queryPageList(areaBuildingFloor);
    }



    /**
    * 分页查询条件
    * @return
    */
    private LambdaQueryWrapper<AreaBuildingFloor> buildQueryWrapper(AreaBuildingFloor bo) {
        LambdaQueryWrapper<AreaBuildingFloor> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPositionName()), AreaBuildingFloor::getPositionName, bo.getPositionName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), AreaBuildingFloor::getType, bo.getType());
        lqw.eq(bo.getParentId() != null, AreaBuildingFloor::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), AreaBuildingFloor::getIcon, bo.getIcon());
        lqw.eq(bo.getFloorCount() != null, AreaBuildingFloor::getFloorCount, bo.getFloorCount());
        lqw.eq(StringUtils.isNotBlank(bo.getCadUrl()), AreaBuildingFloor::getCadUrl, bo.getCadUrl());
        return lqw;
    }

    @Override
    public List<AreaBuildingFloor> treeList(AreaBuildingFloor areaBuildingFloor) {
        LambdaQueryWrapper<AreaBuildingFloor> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(areaBuildingFloor.getType())) {
            if (areaBuildingFloor.getType().equals("B")) {
                queryWrapper.eq(AreaBuildingFloor::getType, "A");
            } else if (areaBuildingFloor.getType().equals("F")) {
                queryWrapper.eq(AreaBuildingFloor::getType, "A")
                        .or().eq(AreaBuildingFloor::getType, "B")
                        .or().eq(AreaBuildingFloor::getType, "U");
            }
        }
        List<AreaBuildingFloor> list = baseMapper.selectList(queryWrapper);
        HashMap<Long, AreaBuildingFloor> map = new HashMap<Long, AreaBuildingFloor>();
        list.stream().forEach(buildingFloorVo -> {
            map.put(buildingFloorVo.getId(), buildingFloorVo);
        });
        List<AreaBuildingFloor> rootNodes = new ArrayList<>();
        list.stream().forEach(buildingFloorVo -> {
            if (buildingFloorVo.getParentId() == 0) {
                rootNodes.add(buildingFloorVo);
            } else {
                AreaBuildingFloor vo = map.get(buildingFloorVo.getParentId());
                vo.getChildren().add(buildingFloorVo);
            }
        });
        return rootNodes;
    }



}
